package com.catersystem.zmy.catersystem.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.catersystem.zmy.catersystem.R;
import com.catersystem.zmy.catersystem.activity.HomeActivity;
import com.catersystem.zmy.catersystem.adapter.BehindBillsAdapter;
import com.catersystem.zmy.catersystem.base.BaseFragment;
import com.catersystem.zmy.catersystem.bean.BehindBillsBean;
import com.catersystem.zmy.catersystem.bean.DishTypeBean;
import com.catersystem.zmy.catersystem.finals.FinalUrl;
import com.catersystem.zmy.catersystem.httputils.GetDataUtils;
import com.catersystem.zmy.catersystem.httputils.HttpUtils;
import com.catersystem.zmy.catersystem.utils.AudioUtils;
import com.catersystem.zmy.catersystem.utils.DateUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;

/**
 * 按单分类显示
 * Created by Administrator on 2017/12/4 0004.
 */

public class AllDishesFragment extends BaseFragment implements BehindBillsAdapter.IGetCookingStatus {

    @BindView(R.id.rv_dishes)
    RecyclerView mRvDishes;

    private Timer mTimer;
    private TimerTask mTimerTask;
    public BehindBillsAdapter billsAdapter;//后厨单据适配器
    public List<BehindBillsBean> billsBeenList = new ArrayList<>(); //模拟后厨单据数据
    private HomeActivity mHomeActivity;


    @Override
    protected View initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
            savedInstanceState) {
        return inflater.inflate(R.layout.fragment_dishes, container, false);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mHomeActivity = (HomeActivity) context;
    }

    @Override
    protected void initView(View view) {
        mRvDishes.setLayoutManager(new GridLayoutManager(mHomeActivity, 4));
        billsAdapter = new BehindBillsAdapter(billsBeenList, this);
        mRvDishes.setAdapter(billsAdapter);
        billsAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.tv_call_out:
                        AudioUtils.getInstance().speakText(((BehindBillsBean) adapter.getData().get(position))
                                .getId() + "号请到后厨取餐！");
                        showLongToast(((BehindBillsBean) adapter.getData().get(position)).getId() + "号请到后厨取餐！");
                        break;
                    case R.id.tv_complete:
                        completeAllOrder(position);
                        break;
                }
            }
        });
        setTimer();
    }

    /**
     * 整单点完成
     **/
    private void completeAllOrder(final int position) {
        Map<String, String> data = new HashMap<>();
        data.put("orgcode", FinalUrl.getOrgCode());
        data.put("listid", billsBeenList.get(position).getId());
        GetDataUtils.getInstance().refreshAllDishData(data, activity, new HttpUtils.IHttpResult() {
            @Override
            public void onSuccess(String result) {
                mHomeActivity.noCookingNum = mHomeActivity.noCookingNum - ((List<BehindBillsBean>) billsAdapter
                        .getData()).get(position).getDetail_uncompleted().size();
                mHomeActivity.tvNoCooking.setText("未烹饪　" + mHomeActivity.noCookingNum + "");
                ((List<BehindBillsBean>) billsAdapter.getData()).remove(position);
                billsAdapter.notifyItemRemoved(position);
                showLongToast("完成");
            }
        });
    }


    /**
     * 设置计时器
     */
    private void setTimer() {
        mTimer = new Timer();
        mTimerTask = new TimerTask() {
            @Override
            public void run() {
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        setDishesWaitTime();

                    }
                });
            }
        };
        mTimer.schedule(mTimerTask, 10000, 10000);
    }

    public void setDishesWaitTime() {
        for (int i = 0; i < billsBeenList.size(); i++) {
            long opertime = (System.currentTimeMillis() / 1000) - billsBeenList.get(i).getOpentime();
            billsBeenList.get(i).setTime(DateUtil.getCutDown1(opertime));
            billsAdapter.notifyDataSetChanged();
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }
    }

    @Override
    public void getCookingStatus(int status) {
        if (status == 1) {
            mHomeActivity.noCookingNum--;
        } else {
            mHomeActivity.noCookingNum++;
        }
        mHomeActivity.tvNoCooking.setText("未烹饪　" + mHomeActivity.noCookingNum + "");
    }
}
